package com.cloudkinto.controller.statistics;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.statistics.NotificationService;
import com.cloudkinto.service.statistics.StatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangyy
 * @date 2020/6/19 9:24
 * @description:
 */
@RestController
@Api(tags = "首页统计模块")
@RequestMapping("/api/admin/census")
public class StatisticsController extends BaseController {

    @Autowired
    private StatisticsService service;
    @Autowired
    private NotificationService notificationService;

    @UserSessionRequired
    @GetMapping("/statistics")
    @ApiOperation(value = "统计", notes = "统计")
    public SingleResult statistics() {
        Long companyId = getCompanyId();
        Long userId = getCurrentUserId();
        return service.statistics(userId, companyId);
    }

    @UserSessionRequired
    @GetMapping("/stockListPage")
    @ApiOperation(value = "库存低于50商品分页", notes = "统计")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult stockListPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String pageSize = request.getParameter("pageSize");

        map.put("companyId", companyId + "");
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("startDate", TimeUtils.getCurrentDateFormat(-365));
        map.put("endDate", TimeUtils.getCurrentDateFormat(1));
        return service.getListPage(map);
    }
    @UserSessionRequired
    @GetMapping("/grownFastProductPage")
    @ApiOperation(value = "库存低于50商品分页", notes = "统计")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult grownFastProductPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String pageSize = request.getParameter("pageSize");

        map.put("companyId", companyId + "");
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("startDate", TimeUtils.getCurrentDateFormat(-60));
        map.put("middleDate", TimeUtils.getCurrentDateFormat(-30));
        map.put("endDate", TimeUtils.getCurrentDateFormat(1));
        return service.getgrownFastListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/notificationList")
    @ApiOperation(value = "通告列表", notes = "统计")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public SingleResult notification(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String pageSize = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", pageSize);
        return SingleResult.success(notificationService.getListPage(map));
    }


    @UserSessionRequired
    @GetMapping("/orderOutNotificationList")
    @ApiOperation(value = "出库单通告", notes = "出库单通告")
    @ApiImplicitParams(@ApiImplicitParam(name = "type", value = "类型"))
    public SingleResult orderOutNotificationList(HttpServletRequest request) {
        return notificationService.orderOutNotificationList(request.getParameter("type"));
    }
}
